﻿namespace MicroCloud.Utils
{
    #region "DES加解密算法帮助类"
    /// <summary>
    /// DES加解密算法帮助类
    /// </summary>
    [Obsolete("方法已过时，请使用AesHelper类中相关方法代替", true)]
    public class DesHelper
    {
        #region "私有常量"
        #region "加密密钥，且必须为8位或16位"
        /// <summary>
        /// 加密密钥，且必须为8位或16位
        /// </summary>
        private const string _secretKey = "SeonSoft";
        #endregion

        #endregion

        #region "方法"
        #region "加密字符串"
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="encryptVal">需要加密的字符串</param>
        /// <param name="key">加密密钥，8位或16位</param>
        /// <returns>加密后的字符串</returns>
        [Obsolete("方法已过时，请使用AesHelper类中相关方法代替", true)]
        public static string Encrypt(string encryptVal, string key = _secretKey)
        {
            encryptVal = string.IsNullOrEmpty(encryptVal) ? "" : encryptVal;
            StringBuilder ret = new();
            try
            {
                using var des = DES.Create();
                byte[] inputByteArray = Encoding.Default.GetBytes(encryptVal);
                des.Key = Encoding.ASCII.GetBytes(key);
                des.IV = Encoding.ASCII.GetBytes(key);

                using var ms = new MemoryStream();
                using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                //ret.ToString();
            }
            catch
            { }
            return ret.ToString();
        }
        #endregion
        #region "解密字符串"
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="decryptVal">需要解密的字符串</param>
        /// <param name="key">解密密钥，8位或16位</param>
        /// <returns>解密后的字符串</returns>
        [Obsolete("方法已过时，请使用AesHelper类中相关方法代替", true)]
        public static string Decrypt(string decryptVal, string key = _secretKey)
        {
            decryptVal = string.IsNullOrEmpty(decryptVal) ? "" : decryptVal;
            using var ms = new MemoryStream();
            try
            {
                using var des = DES.Create();
                byte[] inputByteArray = new byte[decryptVal.Length / 2];
                for (int x = 0; x < decryptVal.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(decryptVal.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key = Encoding.ASCII.GetBytes(key);
                des.IV = Encoding.ASCII.GetBytes(key);

                using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //StringBuilder ret = new StringBuilder();
            }
            catch
            { }
            return Encoding.Default.GetString(ms.ToArray());
        }
        #endregion

        #endregion

    }
    #endregion

}
